package com.lsh.oms.core.service.payment;

import com.lsh.oms.core.dao.payment.BillPaymentInfoDao;
import com.lsh.oms.core.model.payment.BillPaymentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 19/3/21
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
public class BillPaymentService {

    @Autowired
    private BillPaymentInfoDao billPaymentInfoDao;


    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public BillPaymentInfo selectOne(String payTradeId) {

        return billPaymentInfoDao.selectByPayTradeId(payTradeId);
    }

    @Transactional(rollbackFor = Exception.class)
    public int addOrUpdateOne(BillPaymentInfo billPaymentInfo) {
        int flag;
        if (billPaymentInfo.getId() == null) {
            flag = billPaymentInfoDao.insertSelective(billPaymentInfo);
        } else {
            flag = billPaymentInfoDao.updateByPrimaryKeySelective(billPaymentInfo);
        }

        return flag;
    }


    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public List<BillPaymentInfo> selectList(Long billCode) {

        return billPaymentInfoDao.selectByBillCode(billCode);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public List<BillPaymentInfo> selectListBySequences(List<String> payList) {
        if(CollectionUtils.isEmpty(payList)){
            return Collections.EMPTY_LIST;
        }

        return billPaymentInfoDao.selectByPaySequence(payList);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public BillPaymentInfo selectOne(Long billCode, Integer channelType) {

        return billPaymentInfoDao.selectByBillCodeAndChannel(billCode, channelType);
    }

    @Transactional(rollbackFor = Exception.class)
    public int insertOne(BillPaymentInfo billPaymentInfo) {
        if (null == billPaymentInfo.getCreateTime()) {
            billPaymentInfo.setCreateTime(new Date());
            billPaymentInfo.setUpdateTime(new Date());
        }

        return billPaymentInfoDao.insertSelective(billPaymentInfo);
    }


    @Transactional(rollbackFor = Exception.class)
    public int updateByPayTradeId(BillPaymentInfo billPaymentInfo) {
        if (null == billPaymentInfo.getUpdateTime()) {
            billPaymentInfo.setUpdateTime(new Date());
        }

        return billPaymentInfoDao.updateByPayTradeId(billPaymentInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateById(BillPaymentInfo billPaymentInfo) {
        if (null == billPaymentInfo.getUpdateTime()) {
            billPaymentInfo.setUpdateTime(new Date());
        }

        return billPaymentInfoDao.updateByPrimaryKeySelective(billPaymentInfo);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public List<BillPaymentInfo> selectPageList(String uid, Integer start, Integer page,Integer payChannelType) {

        return billPaymentInfoDao.selectPageByFilter(uid, start, page, payChannelType);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public Integer countByFilter(BillPaymentInfo billPaymentInfo) {

        return billPaymentInfoDao.countByFilter(billPaymentInfo);
    }

}
